package vn.gq.tym.tonghopvf;

import vn.gq.tym.model.bean.CategoryItem;
import vn.gq.tym.utils.AdmobAdsHandler;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.view.CategoryFragment;
import vn.gq.tym.view.ChapterFragment;
import vn.gq.tym.widget.BaseFragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.view.Menu;
import com.google.ads.AdView;


public class MainActivity extends BaseActivity implements IClientListener {
	
	private AdmobAdsHandler mAdsHandler;
	private Handler mHandler = new Handler();

	private Runnable showDialogRunnable = new Runnable() {
		
		@Override
		public void run() {
			new BookmarkManager(MainActivity.this).showDialogConfirm();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_layout);
		mAdsHandler = new AdmobAdsHandler((AdView) findViewById(R.id.adView));
		if (!getString(R.string.author).equals("")) {
			getSupportActionBar().setSubtitle(getString(R.string.author));
		}
		BaseFragment fragment;
		if (Configuation.MULTI_STORIES_APP) {
			fragment = CategoryFragment.newInstance("asset://gzip:data");
		} else {
			fragment = ChapterFragment.newInstance(new CategoryItem("asset://gzip:data", ""));
		}
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.add(fragment.getContainerId(), fragment, fragment.getFragmentTag()).commit();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mAdsHandler.loadAds();
	}

	@Override
	protected void onStop() {
		super.onStop();
		mAdsHandler.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mAdsHandler.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		mHandler.removeCallbacks(showDialogRunnable);
		super.onBackPressed();
	}

	@Override
	public void onDone(int code, Object result) {
		mHandler.postDelayed(showDialogRunnable, 100);
	}
	
	
}
